package com.ruoyi.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.user.domain.UserDetail;
import com.ruoyi.user.service.UserDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: houyong
 * @date: 2025年11月14日 15:11
 */
@Tag(name = "用户接口", description = "用户接口")
@RestController
@RequestMapping("/user")
public class UserDetailController {

    @Autowired
    private UserDetailService userDetailService;


    @Operation(summary = "获取用户信息接口", description = "根据用户uid获取用户信息")
    @GetMapping("/info/by/{uid}")
    public AjaxResult getUserDetailByUid(@PathVariable("uid") String uid) {
        UserDetail userDetail = userDetailService.getUserDetailByUid(uid);
        return AjaxResult.success(userDetail);
    }

    @Operation(summary = "重置用户密码接口", description = "管理员重置用户密码")
    @PostMapping("/password/reset")
    public AjaxResult resetPassword(@RequestParam String uid,
                                    @RequestParam String newPassword) {
        try {
            return userDetailService.resetPassword(uid, newPassword);
        } catch (Exception e) {
            return AjaxResult.error("密码重置失败: " + e.getMessage());
        }
    }

    @Operation(summary = "用户修改用户信息接口", description = "用户修改头像、昵称等信息")
    @PostMapping("/info/update")
    public AjaxResult updateUserInfo(@RequestBody UserDetail userDetail) {
        try {
            return userDetailService.updateUserInfo(userDetail);
        } catch (Exception e) {
            return AjaxResult.error("用户信息修改失败: " + e.getMessage());
        }
    }

    @Operation(summary = "用户修改用户信息接口", description = "用户修改头像、昵称等信息")
    @DeleteMapping("/info/delete/{uid}")
    public AjaxResult deleteUserInfo(@PathVariable("uid") String uid) {
        try {
            UserDetail userDetail = userDetailService.getUserDetail(uid);
            userDetail.setStatus(-1);
            return userDetailService.updateUserInfo(userDetail);
        } catch (Exception e) {
            return AjaxResult.error("用户信息修改失败: " + e.getMessage());
        }
    }

    @Operation(summary = "分页查询用户列表接口", description = "分页查询用户列表，支持查询条件有用户手机号，用户邮箱，用户状态，注册时间段进行查询")
    @GetMapping("/list")
    public TableDataInfo getUserList(@RequestParam(required = false) String phone,
                                     @RequestParam(required = false) String email,
                                     @RequestParam(required = false) Integer status,
                                     @RequestParam(required = false) String startTime,
                                     @RequestParam(required = false) String endTime,
                                     @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            // 构造查询条件对象
            UserDetail userDetail = new UserDetail();
            userDetail.setPhone(phone);
            userDetail.setEmail(email);
            userDetail.setStatus(status);
            // 设置时间范围条件
            Map<String, Object> params = new HashMap<>();
            if (startTime != null) {
                params.put("startTime", DateUtils.parseDate(startTime, "yyyy-MM-dd").getTime());
            }
            if (endTime != null) {
                Date endDate = DateUtils.parseDate(endTime, "yyyy-MM-dd");
                params.put("endTime", endDate.getTime() + 24 * 60 * 60 * 1000 - 1);
            }
            IPage<UserDetail> pagingUserList = userDetailService.getPagingUserList(userDetail, params, pageNum, pageSize);
            // 构造返回结果
            TableDataInfo result = new TableDataInfo();
            result.setTotal(pagingUserList.getTotal());
            result.setRows(pagingUserList.getRecords());
            result.setCode(HttpStatus.SUCCESS);
            result.setMsg("查询成功");
            return result;
        } catch (Exception e) {
            TableDataInfo result = new TableDataInfo();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("查询失败: " + e.getMessage());
            return result;
        }
    }

}
